package com.easyctba.core.controller;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.easyctba.common.core.utils.StringUtils;
import com.easyctba.core.domain.bo.FrontProjectBo;
import com.easyctba.core.domain.bo.VideoUploadBo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.easyctba.common.idempotent.annotation.RepeatSubmit;
import com.easyctba.common.log.annotation.Log;
import com.easyctba.common.web.core.BaseController;
import com.easyctba.common.mybatis.core.page.PageQuery;
import com.easyctba.common.core.domain.R;
import com.easyctba.common.core.validate.AddGroup;
import com.easyctba.common.core.validate.EditGroup;
import com.easyctba.common.log.enums.BusinessType;
import com.easyctba.common.excel.utils.ExcelUtil;
import com.easyctba.core.domain.vo.CtbaProjectVo;
import com.easyctba.core.domain.bo.CtbaProjectBo;
import com.easyctba.core.service.ICtbaProjectService;
import com.easyctba.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工程
 * 前端访问路由地址为:/core/project
 *
 * @author Huang
 * @date 2024-02-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project")
public class CtbaProjectController extends BaseController {

    private final ICtbaProjectService ctbaProjectService;

    /**
     * 查询工程列表
     */
    @SaCheckPermission("core:project:list")
    @GetMapping("/list")
    public TableDataInfo<CtbaProjectVo> list(CtbaProjectBo bo, PageQuery pageQuery) {
        return ctbaProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工程列表
     */
    @SaCheckPermission("core:project:export")
    @Log(title = "工程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CtbaProjectBo bo, HttpServletResponse response) {
        List<CtbaProjectVo> list = ctbaProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "工程", CtbaProjectVo.class, response);
    }

    /**
     * 获取工程详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("core:project:query")
    @GetMapping("/{id}")
    public R<CtbaProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(ctbaProjectService.queryById(id));
    }

    /**
     * 新增工程
     */
    @SaCheckPermission("core:project:add")
    @Log(title = "工程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CtbaProjectBo bo) {
        return toAjax(ctbaProjectService.insertByBo(bo));
    }

    /**
     * 修改工程
     */
    @SaCheckPermission("core:project:edit")
    @Log(title = "工程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CtbaProjectBo bo) {
        return toAjax(ctbaProjectService.updateByBo(bo));
    }

    /**
     * 删除工程
     *
     * @param ids 主键串
     */
    @SaCheckPermission("core:project:remove")
    @Log(title = "工程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(ctbaProjectService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 用户获取项目列表
     * @param projectBo 查询参数
     * @param pageQuery 分页对象
     * @return 分页返回实体
     */
    @GetMapping("/front/list")
    public TableDataInfo<CtbaProjectVo> listByUser(FrontProjectBo projectBo, PageQuery pageQuery) {
        return ctbaProjectService.queryPageListByUser(projectBo, pageQuery);
    }

    /**
     * 用户新增项目
     * @param bo 新增参数
     * @return 项目id
     */
    @RepeatSubmit()
    @PostMapping("/front")
    public R<Long> addProject(@Validated(AddGroup.class) @RequestBody FrontProjectBo bo) {
        return ctbaProjectService.insertByUser(bo);
    }

    /**
     * 用户获取项目详情
     * @param id 项目id
     * @return 项目VO对象
     */
    @GetMapping("/front/{id}")
    public R<CtbaProjectVo> getInfoByUser(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(ctbaProjectService.queryByIdAndUser(id));
    }

    /**
     * 获取阿里云OSS的签名信息
     * @return 阿里云要求的签名对象
     */
    @GetMapping("/getOssSign")
    public R<Map<String,Object>> getOssSign(){
        Map<String,Object> signMap= ctbaProjectService.getOssSign();
        if(signMap == null || signMap.isEmpty()){
            return R.fail("未获取到签名，请重试");
        }
        return R.ok(signMap);
    }

    /**
     * 上传视频
     * @param bo 视频上传BO对象
     * @return VOID
     */
    @PutMapping("/uploadVideo")
    public R<Void> uploadVideo(@RequestBody VideoUploadBo bo){
        if (bo==null || StringUtils.isBlank(bo.getUrl()) || StringUtils.isBlank(bo.getType()) || bo.getId()==null){
            return R.fail("参数为空");
        }
        return ctbaProjectService.uploadVideo(bo);
    }

    /**
     * 生成视频封面(弃用)
     * @param file 视频文件流
     * @param type 分析类型
     * @param id 分析id
     * @return 封面地址
     */
    @PostMapping("/setVideoCover/{type}/{id}")
    public R<String> setVideoCover(@RequestPart MultipartFile file,@PathVariable String type,@PathVariable Long id){
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        return ctbaProjectService.setVideoCover(file,type,id);
    }
}
